package com.xiaobawang.homework1225.controller;

import com.xiaobawang.homework1225.entity.JsonResult;
import com.xiaobawang.homework1225.entity.User;
import com.xiaobawang.homework1225.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * description：User Controller
 * author：Synhard
 * create time：2019/12/25 09：34
 * last modified: 2019/12/25 09：34
 * **/
@Api(tags = "user")
@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value = "Add user")
    @PutMapping("/addUser")
    public JsonResult addUser(String u_name, String u_pwd, String u_email, Date u_create_time) {
        JsonResult result = new JsonResult();
        if (userMapper.selectUserByName(u_name) != null) {
            // 如果用户名已存在，则提示添加用户失败
            result.setErrorcode(-1);
            result.setStatus("添加用户失败，用户名重复！");
            result.setResults(null);
        } else {
            // 如果用户名不存在则尝试插入新的用户
            if(userMapper.insertUser(u_name, u_pwd, u_email, u_create_time) == 1) {
                // 如果插入用户成功
                result.setErrorcode(0);
                result.setStatus("添加用户成功!");
                result.setResults(userMapper.selectUserByName(u_name));
            } else {
                // 出现了其他错误
                result.setErrorcode(-2);
                result.setStatus("添加用户失败，出现其他错误！");
                result.setResults(null);
            }

        }
        return result;
    }

    @ApiOperation("Delete user by ID")
    @GetMapping("/deleteUser")
    public JsonResult deleteUser(int u_id) {
        JsonResult result = new JsonResult();
        if (userMapper.deleteUser(u_id) == 1) { // 如果成功删除用户
            result.setStatus("删除用户成功!");
            result.setErrorcode(0);
            result.setResults(null);
        } else { // 如果删除未成功，用户不存在
            result.setStatus("删除用户失败!用户不存在");
            result.setErrorcode(-1);
            result.setResults(null);
        }
        return result;
    }

    @ApiOperation("Alter user")
    @PutMapping("/alterUser")
    public JsonResult alterUser(User user) {
        JsonResult result = new JsonResult();
        if (userMapper.selectUserByName(user.getU_name()) != null) { // 修改的用户名称已重复
            result.setErrorcode(-1);
            result.setStatus("用户名已存在，请换一个吧");
            result.setResults(null);
        } else {
            if (userMapper.alterUser(user) == 1) { // 修改用户信息成功
                result.setErrorcode(0);
                result.setStatus("用户信息修改成功！");
                result.setResults(userMapper.selectUserById(user.getU_id()));
            } else {
                result.setErrorcode(-1);
                result.setStatus("用户信息修改失败！");
                result.setResults(null);
            }
        }
        return result;
    }

    @ApiOperation("Select user by ID")
    @GetMapping("/selectUseById")
    public JsonResult selectUserById(int u_id) {
        JsonResult result = new JsonResult();
        User u = userMapper.selectUserById(u_id);
        if (u != null) {
            result.setStatus("查询用户信息成功！");
            result.setErrorcode(0);
            result.setResults(u);
        } else {
            result.setStatus("用户不存在！");
            result.setErrorcode(-1);
            result.setResults(null);
        }
        return result;
    }

    @ApiOperation("Fuzzy select user by name")
    @GetMapping("/fuzzySelectUserByName")
    public JsonResult fuzzySelectUserByName(String u_name) {
        JsonResult result = new JsonResult();
        result.setErrorcode(0);
        result.setStatus("查询成功！");
        result.setResults(userMapper.fuzzySelectUserByName(u_name));
        return result;
    }

    @ApiOperation("Select user by create time")
    @PutMapping("/selectUserByTime")
    public JsonResult selectUserByTime(Date begin, Date end) {
        JsonResult result = new JsonResult();
        result.setErrorcode(0);
        result.setStatus("查询成功！");
        result.setResults(userMapper.selectUserByTime(begin, end));
        return result;
    }

    @ApiOperation("Select user`s name, email. role, create time")
    @GetMapping("/selectUserInfo")
    public JsonResult selectUserInfo() {
        JsonResult result = new JsonResult();
        result.setStatus("查询成功！");
        result.setErrorcode(0);
        result.setResults(userMapper.selectUserInfo());
        return result;
    }

}
